﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace prog_tech_5
{
    public partial class Form1 : Form
    {

        private MusicList music_list = new MusicList();
        ILoggerFactory factory = new FileLoggerFactory();
        ILogger logger;
        AudioRecordFactory podcast_creators = new PodcastFactory();
        TracksFactory[] music_tracks_creators = { new SongFactory(), new ClassicFactory() };

        private void updateTotalLengthText()
        {
            label7.Text = "Продолжительность списка: " + music_list.CalculateTime().ToString();
        }

        private void updateListBox()
        {
            listBox1.SelectedIndex = -1;
            listBox1.Items.Clear();
            foreach (AudioRecord rec in music_list.tracks())
            {
                listBox1.Items.Add(rec.author + " - " + rec.title);
            }
        }

        public Form1()
        {
            InitializeComponent();
            updateTotalLengthText();
            logger = factory.CreateLogger("test.log");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            music_list.clearList();
            logger.Log("Clear list");
            updateTotalLengthText();
            updateListBox();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int to_delete_index = listBox1.SelectedIndex;
            if (to_delete_index != -1)
            {
                music_list.RemoveTrack(to_delete_index);
                updateTotalLengthText();
                updateListBox();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                comboBox2.Enabled = true;
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                comboBox2.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string title = textBox1.Text;
            double time = double.Parse(textBox2.Text);
            string author = textBox3.Text;
            if (comboBox1.SelectedIndex == 0)
            {
                if (comboBox2.SelectedIndex == 0)
                {
                    music_list.AddTrack(music_tracks_creators[0].CreateMusicTrack(title, author, time));
                    logger.Log("Add song to list");
                }
                else if (comboBox2.SelectedIndex == 1)
                {
                    music_list.AddTrack(music_tracks_creators[1].CreateMusicTrack(title, author, time));
                    logger.Log("Add classic to list");
                }
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                music_list.AddTrack(podcast_creators.CreateRecord(title, author, time));
                logger.Log("Add podcast to list");
            }
            updateListBox();
            updateTotalLengthText();
        }
    }
}
